package com.wmz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wmz.constant.Constants;
import com.wmz.entity.User;
import com.wmz.query.UserQuery;
import com.wmz.service.UserService;
import com.wmz.mapper.UserMapper;
import com.wmz.util.JWTUtils;
import io.netty.util.internal.StringUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
* @author q1186
* @description 针对表【t_user(用户表)】的数据库操作Service实现
* @createDate 2024-02-15 21:42:41
*/
@Service
public class UserServiceImpl implements UserService{
    @Resource
    private  UserMapper userMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    /**
     * 登录验证
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user =  userMapper.selectByLoginAct(username);
        if(user == null){
            throw new UsernameNotFoundException("登录账号不存在！！！");
        }
        return user;
    }

    /**
     * 分页查询
     * @param pageNum
     * @return
     */
    @Override
    public PageInfo<User> getUserByPage(Integer pageNum) {
        //1、开始分页查询
        PageHelper.startPage(pageNum, Constants.PAGE_SIZE);
        //2、查询
        List<User> userList = userMapper.selectUserByPage();
        //3、封装分页数据到PageInfo
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        return userPageInfo;
    }

    /**
     * 通过id获取用户
     * @param id
     * @return
     */
    @Override
    public User getUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    /**
     * 新增用户
     * @param userQuery
     * @return
     */
    @Override
    public int insertUser(UserQuery userQuery) {
        User user = new User();
        BeanUtils.copyProperties(userQuery,user);
        //密码加密
        user.setLoginPwd(passwordEncoder.encode(userQuery.getLoginPwd()));
        //设置创建时间
        user.setCreateTime(new Date());
        //设置创建人
        //通过token获取当前用户的id
        Integer userId = JWTUtils.parseJWTByUserId(userQuery.getToken());
        user.setCreateBy(userId);
        return userMapper.insertSelective(user);
    }

    /**
     * 编辑用户
     * @param userQuery
     * @return
     */
    @Override
    public int updateUser(UserQuery userQuery) {
        User user = new User();
        BeanUtils.copyProperties(userQuery,user);

        if(StringUtils.hasText(userQuery.getLoginPwd())){
            //密码存在，进行密码加密
            user.setLoginPwd(passwordEncoder.encode(userQuery.getLoginPwd()));
        }
        //设置修改时间
        user.setEditTime(new Date());
        //设置修改人
        //通过token获取当前用户的id
        Integer userId = JWTUtils.parseJWTByUserId(userQuery.getToken());
        user.setEditBy(userId);

        return userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    public int delUser(Integer id) {
        return userMapper.delUser(id);
    }

    @Override
    public int batchDelUser(List<String> idList) {
        return userMapper.batchDelUser(idList);
    }
}




